import 'package:boss/common/common.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/page/searchCommodity/search_commodity_provider.dart';
import 'package:boss/ui/page/commodity/categories/widget/categoriesCommodityBrowser/categories_commodity_item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flustars/flustars.dart' as flustars;

class SearchCommodityList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Consumer<SearchCommodityProvider>(
        builder: (context, provider, _) {
          if (provider.searchCommoditys.length == 0) {
            return Center(
              child: Text(
                '没有查到相关菜品～',
                style: TextStyleConstant().normal_3_28,
              ),
            );
          }

          return ListView.builder(
            itemBuilder: (context, index) {
              var current = provider.searchCommoditys[index];

              return CategoriesCommodityItem(
                index: index,
                data: current,
                isHideIcon: true,
                onTap: () async {
                  var history = flustars.SpUtil.getStringList(Constant.spSearchCommodity);
                  if (!history.contains(current.title)) {
                    var result = await flustars.SpUtil.putStringList(
                      Constant.spSearchCommodity,
                      [...history, current.title],
                    );
                    print('存储搜索菜品历史${result ? '成功' : '失败'}');
                  }
                  Navigator.pop(context, current);
                },
              );
            },
            itemCount: provider.searchCommoditys.length,
          );
        },
      ),
    );
  }
}
